<?php
declare(strict_types=1);
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/5/10
 * Time: 11:25
 */

namespace App\Controller\User;

use App\Controller\AppBaseController;
use App\Model\Article;
use App\Model\ArticleColumn;
use App\Model\Collect;
use App\Model\MultiCollect;
use App\Model\MultiLearn;
use App\Model\MultiZan;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\WxApiAuth;

#[Controller(prefix: 'user')]
#[Middleware(WxApiAuth::class)]
class ArticleController extends AppBaseController
{

    /**
     * 专栏文章列表
     * @param int|null $column_id
     * @return object
     */
    #[RequestMapping(path:'articleList[/{column_id}]',methods: 'get')]
    public function articleList(int|null $column_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);

            $page = $this->request->input('page', 1);
            $limit = $this->request->input('limit', 20);
            $offset = ($page - 1) * $limit;

            $where['status'] = 1;
            $where['is_long'] = 1;

            $dateTime = date('Y-m-d H:i:s',time());
            if($column_id){
                $where['column_id'] = $column_id;
                $whereOr = [
                    ['status','=',1],
                    ['column_id','=',$column_id],
                    ['show_start_at','<=',$dateTime],
                    ['show_end_at','>=',$dateTime],
                ];
            }else{
                $whereOr = [
                    ['status','=',1],
                    ['show_start_at','<=',$dateTime],
                    ['show_end_at','>=',$dateTime],
                ];
            }
            $list = Article::where($where)
                ->orWhere($whereOr)
                ->selectRaw('id,title,type,column_id,photo,link_url,file_list,created_at')
                ->orderByDesc('id')
                ->offset($offset)
                ->limit($limit)
                ->get();

            $next = 0;
            if($list->isNotEmpty()){
                if(count($list)>$limit) $next = 1;
            }
            return $this->reMsg->re(1,'success',['list' => $list, 'next' => $next]);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }


    /**
     * 专栏文章详情
     * @param int $article_id
     * @return object
     */
    #[RequestMapping(path:'articleInfo/{article_id}',methods: 'get')]
    public function articleInfo(int $article_id): object
    {
        try{
            //验证用户信息
            $checkInfo = $this->checkUser();
            if($checkInfo['status']==40002) return $this->reMsg->re(10002,$checkInfo['message']);
            $_user = $checkInfo['data'];
            
            $info = Article::where('id',$article_id)->selectRaw('id,title,photo,content,media_list,file_list,created_at')->first();
            if(empty($info)) return $this->reMsg->re(10008);

            $info->is_collect = 0;
            $info->is_learn = 0;
            $info->is_zan = 0;
            //处理用户是否收藏
            $whereDo['module'] = 'article';
            $whereDo['relation_id'] = $info->id;

            //获取点赞数量
            $info->zan_count = MultiZan::where($whereDo)->count();

            $whereDo['uid'] = $_user->id;
            //判断是否学习
            $learnInfo = MultiLearn::where($whereDo)->first();
            if($learnInfo) $info->is_learn = 1;
            //判断是否收藏
            $whereDo['status'] = 1;
            $collectInfo = MultiCollect::where($whereDo)->first();
            if($collectInfo) $info->is_collect = 1;
            //判断是否点赞
            $zanInfo = MultiZan::where($whereDo)->first();
            if($zanInfo) $info->is_zan = 1;

            return $this->reMsg->re(1,'success',$info);
        }catch (\Exception $exception){
            return $this->diyError->errorJson($exception);
        }
    }



}